package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    @Insert("insert into orders values (null, #{number}, #{status}, #{userId}, #{addressBookId}" +
            ", #{orderTime}, #{checkoutTime}, #{payMethod}, #{payStatus}, #{amount}, #{remark}, #{phone}" +
            ", #{address}, #{userName}, #{consignee}, #{cancelReason}, #{rejectionReason}, #{cancelTime}," +
            " #{estimatedDeliveryTime}, #{deliveryStatus}, #{deliveryTime}, #{packAmount}, #{tablewareNumber}" +
            ", #{tablewareStatus})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void save(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Update("update orders set status = #{orderStatus},pay_status = #{orderPaidStatus} ,checkout_time = #{checkOutTime} " +
            "where number = #{orderNumber}")
    void updateStatus(Integer orderStatus, Integer orderPaidStatus, LocalDateTime checkOutTime, String orderNumber);

    @Select("select * from orders where id = #{id}")
    Orders findById(Long id);


    /**
     * 分页查询
     *
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 统计状态
     * @param status
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer countStatistics(Integer status);

    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> findByLongTime(Integer status, LocalDateTime orderTime);

    /**
     * 获取营业额统计数据
     * @param map
     * @return
     */
    Double sumByMap(Map<String, Object> map);

    Integer ordersStatistics(Map<String, Object> map);

    List<GoodsSalesDTO> SalesTop10(LocalDateTime beginTime, LocalDateTime endTime);

    @Select("select count(*) from orders")
    Integer countAll();
}
